<template>
    <div style="position: relative">
        <!--                <div  class="state_info">{{state_map[state_code]}} &#45;&#45; {{stateType}}</div>-->
        <!--        {{state_map[state_code].includes('驳回')}}-->
        <!-- {{exam_org}} -->
        <!-- {{is_pass}} -->
        <!--              {{stateStr}}-->
        <!--      {{signed}}-->
<!--        待审{{findkey}}&#45;&#45;{{state_code}}-->
<!--        {{isExam}}-->
        <div v-if="state_map[state_code].slice(0,4) == stateType" class="hover">
            <div v-if="user_type===2 && currentMessage && currentOrg">
                <el-tooltip :disabled="!(currentMessage.audit_log.length > 0 && currentMessage.audit_log[0].note)" class="item" effect="dark"
                            :content="`${currentMessage.audit_log.length > 0 ? currentMessage.audit_log[0].note || '' : ''}`" placement="bottom">
                    <div v-if="stateStr==='orgWaiting_1'">
                        <div style="display: flex;width: 100%;justify-content: space-around;">
                            <div class="checkButton" @click="org_reject_1">
                                <el-button class="stateButton" type="danger" size="mini" icon="el-icon-close"></el-button>
                                <!--                                <img src="../assets/check/error.png">-->
                            </div>
                            <div class="checkButton" @click="org_pass_1">
                                <el-button class="stateButton" type="success" size="mini" icon="el-icon-check"></el-button>
                                <!--                                <img src="../assets/check/success.png">-->
                            </div>
                        </div>
                    </div>
                </el-tooltip>
                <el-tooltip :disabled="!(currentMessage.audit_log.length > 0 && currentMessage.audit_log[0].note)" class="item" effect="dark"
                            :content="`${currentMessage.audit_log.length > 0 ? currentMessage.audit_log[0].note || '' : ''}`" placement="bottom">
                    <div v-if="stateStr==='orgWaiting_2' && currentMessage.exam_org && currentOrg.org_code === currentMessage.exam_org.org_code">
                        <!--                  <div v-if="signed">-->
                        <!--                    <div v-if="isExam !== null" style="display: flex;width: 100%;justify-content: space-around;align-items: center;">-->
                        <!--                                              <div class="checkButton" @click="org_reject_2(isExam)">-->
                        <!--                                                <el-button class="stateButton" type="danger" size="mini" icon="el-icon-close"></el-button>-->
                        <!--                                              </div>-->
                        <!--                                              <div class="checkButton" @click="org_pass_2(isExam)">-->
                        <!--                                                <el-button class="stateButton" type="success" size="mini" icon="el-icon-check"></el-button>-->
                        <!--                                              </div>-->
                        <!--                    </div>-->
                        <!--                    <div v-if="isExam == null">-->
                        <!--                      <el-tag type="warning">待报考</el-tag>-->
                        <!--                    </div>-->
                        <!--                  </div>-->
                        <!--                    <div v-else>-->
                        <!--                        <el-tag>合同未签署</el-tag>-->
                        <!--                    </div>-->
                        <!-- <div v-if="isExam !== null">
                            <el-tag type="warning">待审</el-tag>
                        </div> -->
                        <!--                  //未报考的时候（报考时间NONE）待报考；-->
                        <!--            // 机构二审待审 已签字，显示操作按钮 ；合同未签署-->
<!--                        当前机构{{currentOrg_code}}-->
                        <div v-if="!currentMessage.exam_date">
                            <el-tag type="warning">待报考</el-tag>
                        </div>
                        <div v-else>
                            <div style="display: flex" v-if="(currentMessage.state_code>=4 ||currentMessage.state_code<=6 ) && currentMessage.signed">
                                <div class="checkButton" style="margin-right: 5px" @click="org_reject_2(isExam)">
                                    <el-button class="stateButton" type="danger" size="mini" icon="el-icon-close"></el-button>
                                </div>
                                <div class="checkButton" @click="org_pass_2(isExam)">
                                    <el-button class="stateButton" type="success" size="mini" icon="el-icon-check"></el-button>
                                </div>
                            </div>
                            <div v-else-if="!currentMessage.signed">
                                <el-tag type="primary">合同未签署</el-tag>
                            </div>
                            <div v-else-if="currentMessage.state_code>=7">
                                <el-tag type="success">通过</el-tag>
                            </div>
                        </div>
                    </div>
                </el-tooltip>
                <div v-if="stateStr==='adminWaiting_1'||stateStr==='adminWaiting_2'">
                    <el-tag type="warning">审核中</el-tag>
                </div>
            </div>
            <div v-if="user_type===3 && currentMessage">
                <el-tooltip :disabled="!(currentMessage.audit_log.length > 0 && currentMessage.audit_log[0].note)" class="item" effect="dark"
                            :content="`${currentMessage.audit_log.length > 0 ? currentMessage.audit_log[0].note || '' : ''}`" placement="bottom">
                <div v-if="stateStr==='adminWaiting_1'">
                    <div style="display: flex;width: 100%;justify-content: space-around;">
                        <div class="checkButton" @click="admin_reject_1">
                            <el-button type="danger" size="mini" icon="el-icon-close"></el-button>
                        </div>
                        <div class="checkButton" @click="admin_pass_1">
                            <el-button type="success" size="mini" icon="el-icon-check"></el-button>
                        </div>
                    </div>
                </div>
                <div v-if="stateStr==='adminWaiting_2'">
                    <div v-if="currentMessage.signed" style="display: flex;width: 100%;justify-content: space-around;">
                        <div class="checkButton" @click="admin_reject_2">
                            <el-button type="danger" size="mini" icon="el-icon-close"></el-button>
                        </div>
                        <div class="checkButton" @click="admin_pass_2">
                            <el-button type="success" size="mini" icon="el-icon-check"></el-button>
                        </div>
                    </div>
                    <div v-if="!currentMessage.signed">
                        <el-tag type="primary">合同未签署</el-tag>
                    </div>
                </div>
                </el-tooltip>
                <div v-if="stateStr==='orgWaiting_1'||stateStr==='orgWaiting_2'">
                    <el-tag v-if="isExam == null" type="warning">待报考</el-tag>
                    <el-tag v-if="isExam != null" type="warning">审核中</el-tag>
                </div>
            </div>
        </div>
        <div v-else>
            <div v-if="state_code < findkey">
                <el-tag v-if="isExam != null">待审</el-tag>
                <el-tag type="warning" v-if="isExam == null">待报考</el-tag>
            </div>
            <div v-if="state_code > findkey">
                <el-tag type="success">通过</el-tag>
                <el-tag v-if="is_pass > 0" type="success">补考</el-tag>
            </div>
        </div>
        <el-dialog
                v-if="stateReject"
                title="驳回原因"
                :visible.sync="centerDialogVisible"
                width="30%"
                center>
            <el-tabs class="rejectModel" type="border-card">
                <el-tab-pane label="信息驳回">
                    <el-input v-model="rejectContent"></el-input>
                    <div style="margin-top: 20px">
                        <el-button @click="close">取 消</el-button>
                        <el-button type="primary" :disabled="rejectContent == '' ? true:false" @click="rejectSubmit">确 定</el-button>
                    </div>
                </el-tab-pane>
                <el-tab-pane v-if="currentMessage.contract_id" label="合同驳回">
                    <el-input v-model="contractRejectContent" clearable></el-input>
                    <div style="margin-top: 20px">
                        <el-button @click="close">取消</el-button>
                        <el-button :disabled="contractRejectContent == '' ? true:false" @click="rejectContentFun">确认</el-button>
                    </div>
                </el-tab-pane>
            </el-tabs>
        </el-dialog>
    </div>
</template>

<script>
    import {check, getOrg} from "../request/api";

    export default {
        name: "Item",
        props: {
            signed:{
                type:Boolean,
                default:false
            },
            exam_org:{
                type:Object,
                default:function(){
                    return {}
                }
            },
            currentOrg:{
                type:Object,
                default:function () {
                    return {}
                }
            },
            state_code: {
                type: Number
            },
            pid: {
                type: Number
            },
            stateType: {
                type: String
            },
            currentMessage: {
                type: Object,
                default:() =>{}
            },
            is_pass:{
                type:Number,
                default:0
            },
            isExam:{
                type: Object,
                default:() =>{
                    return {}
                }
            }
        },
        data() {
            return {
                contractRejectContent:'字迹不清晰',
                stateReject: null,
                rejectContent: '',
                centerDialogVisible: false,
                // itemIsShow:false,
                state_map: {
                    0: '机构一审驳回',
                    1: '机构一审',
                    2: '机构一审驳回',
                    3: '总部一审',
                    4: '机构二审驳回',
                    5: '机构二审',
                    6: '机构二审驳回',
                    7: '总部二审',
                    8: '全部审核通过'
                },
                user_type: this.$store.state.user_type,
            }
        },
        created(){

        },
        beforeCreate(){
            console.log('item',this.$store.state.user_type);
        },
        computed: {
            stateStr() {
                switch (this.state_code) {
                    case 0:
                        return 'orgWaiting_1';
                    case 1:
                        return 'orgWaiting_1';
                    case 2:
                        return 'orgWaiting_1';
                    case 3:
                        return 'adminWaiting_1';
                    case 4:
                        return 'orgWaiting_2';
                    case 5:
                        return 'orgWaiting_2';
                    case 6:
                        return 'orgWaiting_2';
                    case 7:
                        return 'adminWaiting_2';
                    case 8:
                        return 'allPass'

                }
                return null

            },
            findkey() {
                for (let item in this.state_map) {
                    if (this.stateType == this.state_map[item]) {
                        return item;
                    }
                }
                return null;
            },
            getuserType(){
                this.user_type = this.$store.state.user_type;
            }
        },
        methods: {
            //未报考的时候（报考时间NONE）待报考；
            // 机构二审待审 已签字，显示操作按钮 ；合同未签署

            rejectContentFun(){
                let params = {pid: this.pid, method: this.$store.state.user_type == 2 ?'org_contract_reject':'admin_contract_reject',note: '驳回原因（报考确认书）：' + this.contractRejectContent}
                check(params).then(res => {
                    console.log(res);
                    this.close();
                    this.$emit('chekcSuccess')
                    this.$message({
                        type: 'success',
                        message: '合同驳回成功!'
                    });
                }).catch(err =>{
                    this.$message({type:'error',message:err.msg+'，'+err.errors})
                })
            },

            close() {
                this.centerDialogVisible = false
            },
            rejectSubmit() {
                let params = {pid: this.pid, method: this.stateReject, note: '驳回原因（资料）：' + this.rejectContent}
                console.log(params.note.length)
                if (params.note.length > 90) {
                    this.$message({
                        type: "error",
                        message: "驳回原因不能大于15个字符"
                    })
                    return;
                }
                check(params).then(res => {
                    console.log(res);
                    this.rejectContent = "";
                    this.close();
                    this.$emit('chekcSuccess')
                    this.$message({
                        type: 'success',
                        message: '驳回成功!'
                    });
                }).catch(err =>{
                    this.$message({type:'error',message:err.msg+'，'+err.errors})
                })
            },
            //机构一审驳回
            org_reject_1() {
                console.log('org_reject_1');
                this.stateReject = 'org_reject_1';
                this.centerDialogVisible = true;
                // let params = {pid:pid,method:'org_reject_1'}
                // check().then(res =>{
                //
                // })
            },
            org_pass_1() {
                console.log('org_pass_1')
                let params = {pid: this.pid, method: 'org_pass_1'}
                this.$confirm('是否核实并允许通过该审核', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    check(params).then(res => {
                        console.log(res);
                        this.$emit('chekcSuccess')
                        this.$message({
                            type: 'success',
                            message: '提交成功!'
                        });
                    }).catch(err =>{
                        this.$message({type:'error',message:err.msg+'，'+err.errors})
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消'
                    });
                });
            },
            org_reject_2(isExam) {
                console.log('org_reject_2',isExam)
                if(isExam == null){
                    this.$message({
                        type:'error',
                        message:'该用户还未报名'
                    })
                    return
                }else{
                    this.stateReject = 'org_reject_2';
                    this.centerDialogVisible = true;
                }
            },
            org_pass_2(isExam) {
                console.log('org_pass_2',isExam);
                if(isExam == null){
                    this.$message({
                        type:'error',
                        message:'该用户还未报名'
                    })
                    return
                }else{
                    let params = {pid: this.pid, method: 'org_pass_2'}
                    this.$confirm('是否核实并允许通过该审核', '提示', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning'
                    }).then(() => {
                        check(params).then(res => {
                            console.log(res);
                            this.$emit('chekcSuccess')
                            this.$message({
                                type: 'success',
                                message: '提交成功!'
                            });
                        }).catch(err =>{
                            this.$message({type:'error',message:err.msg+'，'+err.errors})
                        })
                    }).catch(() => {
                        this.$message({
                            type: 'info',
                            message: '已取消'
                        });
                    });
                }
            },
            admin_reject_1() {
                console.log('admin_reject_1');
                this.stateReject = 'admin_reject_1';
                this.centerDialogVisible = true;
            },
            admin_pass_1() {
                console.log('admin_pass_1');
                let params = {pid: this.pid, method: 'admin_pass_1'}
                this.$confirm('是否核实并允许通过该审核', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    check(params).then(res => {
                        console.log(res);
                        this.$emit('chekcSuccess')
                        this.$message({
                            type: 'success',
                            message: '提交成功!'
                        });
                    }).catch(err =>{
                        this.$message({type:'error',message:err.msg+'，'+err.errors})
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消'
                    });
                });
            },
            admin_reject_2() {
                console.log('admin_reject_2')
                this.stateReject = 'admin_reject_2';
                this.centerDialogVisible = true;
            },
            admin_pass_2() {
                console.log('admin_pass_2');
                let params = {pid: this.pid, method: 'admin_pass_2'}
                this.$confirm('是否核实并允许通过该审核', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    check(params).then(res => {
                        console.log(res);
                        this.$emit('chekcSuccess')
                        this.$message({
                            type: 'success',
                            message: '提交成功!'
                        });
                    }).catch(err =>{
                        this.$message({type:'error',message:err.msg+'，'+err.errors})
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消'
                    });
                });
            }
        }
    }
</script>

<style>
    .rejectModel .el-tabs__nav{
        width: 100%;
    }
    .rejectModel .el-tabs__item{
        width: 50%;
        text-align: center;
    }
</style>

<style scoped>
    .stateButton .el-button--mini{
        padding: 7px 7px;
    }
    .item{
        margin-bottom: 0px;
    }
    .checkButton{
        cursor: pointer;
    }
</style>
